const { $Toast } = require('../../dist/base/index');
const $api = require('../../utils/api.js').API;
Page({
    data: {
        show: false,
        userinfo: {},
        gender: ['男', '女'],
        genderIndex: -1,
        progress: 0,
        value: '',
        title: ['昵称', '宿舍', '手机号', '个性签名'],
        titleIndex: -1,
        targetAttri: ['name', 'drom', 'contact', 'signature'],
        imgUrl: ''
    },
    onLoad(e) {
        console.log('userinfo onload')
        let userinfo = JSON.parse(e.userinfo)
        this.setData({
            userinfo,
            genderIndex: userinfo.gender,
        }),
            this.countProgress()
    },
    // 更换性别
    genderChange: function (e) {
        let genderIndex = e.detail.value
        this.setData({
            genderIndex,
            'userinfo.gender': genderIndex
        })
    },

    // 弹窗点击取消
    onClose() {
        this.setData({ show: false });
    },

    // 打开弹窗
    onShowDialog(e) {
        this.setData({
            show: true,
            titleIndex: e.currentTarget.dataset.index,
            value: e.currentTarget.dataset.value
        })
    },

    // 弹窗点击确定
    onConfirm() {
        let target = `userinfo.${this.data.targetAttri[this.data.titleIndex]}`
        console.log(target)
        this.setData({
            show: false,
            [target]: this.data.value
        });
    },

    // 输入框改动内容
    onChange(e) {
        this.setData({
            value: e.detail
        })
    },

    // 保存修改（更新到数据库）
    saveEdit(e) {
        let newUser = e.currentTarget.dataset.userinfo
        $api.updateUser({
            newUser
        })
            .then(res => {
                this.countProgress()
                $Toast({
                    content: '保存成功',
                    type: 'success'
                });
            })
            .catch(err => console.log(err))
    },

    // 上传头像后
    afterRead(e) {
        let imgUrl = e.detail.file.url
        this.setData({
            imgUrl
        })
        this.setData({
            'userinfo.profilePic': imgUrl
        })
        wx.uploadFile({
            url: `http://localhost:3000/user/upload`,
            filePath: this.data.imgUrl,
            formData: { openid: this.data.userinfo.openid },
            name: 'img',
        });
    },

    // 计算信息完善度
    countProgress() {
        this.setData({
            progress: 0
        })
        if (this.data.userinfo.contact) {
            this.setData({
                progress: this.data.progress += 20
            })
        }
        if (this.data.userinfo.drom) {
            this.setData({
                progress: this.data.progress += 20
            })
        }
        if (this.data.gender) {
            this.setData({
                progress: this.data.progress += 20
            })
        }
        if (this.data.userinfo.name) {
            this.setData({
                progress: this.data.progress += 20
            })
        }
        if (this.data.userinfo.signature) {
            this.setData({
                progress: this.data.progress += 20
            })
        }
    },

    // 一键复制openid
    copyOpenid() {
        wx.setClipboardData({
            // data: this.data.userinfo.openid,
            data: 'data',
            success (res) {
              wx.getClipboardData({
                success (res) {
                  console.log(res.data) // data
                }
              })
            }
        })
    }
})